<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="7">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { getRoleList } from '@/api/setting'
import { assignRole } from '@/api/empliyees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false // 默认
    },
    userId: { // 传当前点击目标的id
      type: String,
      default: null
    }
  },
  data() {
    return {
      list: [], // 存储所有用户角色信息
      roleIds: [] // 负责存储当前用户所拥有的角色id
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id) // 获取员工基本信息 解构出用户对应的角色
      this.roleIds = roleIds // 赋值角色信息
    },
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 20 }) // 获取用户角色列表
      this.list = rows
    },
    async btnOK() {
      await assignRole({ id: this.userId, roleIds: this.roleIds })
      this.$emit('update:showRoleDialog', false)
    },
    btnCancel() {
      this.roleIds = [] // 清空原来的数组
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
